#!/usr/bin/env ruby
#
#  Created by Jerry Luk on 2007-09-03.
#  Copyright (c) 2007. All rights reserved.
module Google
  module UrlUtils
    def form_encoded(hash)
      encoded = []
      hash.each do |key, value|
        if value.kind_of?(Array)
          value.each do | query_value |
            our_key = CGI.escape(key.to_s.dasherize)
            encoded << our_key + "=" + uri_escape(query_value)
          end
        else
          encoded << CGI.escape(key.to_s.dasherize) + '=' + uri_escape(value)
        end
      end
      joined = encoded.join('&')
      return joined
    end

    def url_signature(url, shared_key)
      incr_digest = Digest::MD5.new()
    	incr_digest << url
    	incr_digest << shared_key			
    	sig = incr_digest.hexdigest.to_s
    end
    
    def uri_escape(value = nil)
      URI.escape(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
    end
    
  end
end